package com.logos.servlet.rep;

import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 响应字节数据：设置字节数据的响应体
 */
@WebServlet("/resp2")
public class ResponseTest02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.读取文件
        FileInputStream fis = new FileInputStream("C://Users//rose//Videos//headImage2.jpg");
        // 2.获取字节输出流
        ServletOutputStream sos = resp.getOutputStream();
        // 3.完成流的copy
//        byte[] bytes = new byte[1024];
//        int len = 0;
//        while((len = fis.read(bytes)) != -1) {
//            sos.write(bytes,0,len);
//        }
//        fis.close();

        IOUtils.copy(fis,sos);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
